
var _m_url = window.parent._index_path;


var divNames=new Array();
var width=new Array();
var height=new Array();
var dsIds=new Array();
var colns=new Array();

function loadClientInfo(){
	divNames[0]="sdf_DataList";
	divNames[1]="noCheckDataList";
	divNames[2]="yesCheckDataList";
	
	width[0]=260;
	width[1]=1000;
	width[2]=1000;
	
	height[0]=700;
	height[1]=270;
	height[2]=270;
	
	
	dsIds[0]="ds_fx_hand_out1";
	dsIds[1]="ds_fx_hand_out2";
	dsIds[2]="ds_fx_hand_out3";
	
	
	//客户信息的字段
	colns[0]=[[
		          {field:'kk',checkbox:true}, 
//		          {field:"ZD_TYPE",title:'征订类型',width:75},
		          {field:"C_ID",title:'客户',align:'left',formatter:eval('window.parent.dz_clientsfmat'),width:230},
//		          {field:"LAST_SEND_DATE",title:'最迟发货日',width:100}
		          ]];
	
	//待验区的列表字段
	colns[1]=[[
		          {field:"H_ISBN",title:'书号',width:120},
		          {field:"RUN_ID",title:'该单据的唯一键',hidden:true},
		          {field:"H_NAME",title:'书名',width:200},
		          {field:"H_PRICE",title:'定价',align:'right',width:80},
		          {field:"H_AMOUNT_YF",title:'应发数',align:'right',width:65},
		          {field:"H_AMOUNT",title:'本次数',align:'right',editor:{type:'numberbox',options: {pre:'sdfn',"divIdSub":'noCheckDataList'}},width:65},
//		          {field:"USTOCK_NO",title:'可用库存数',align:'right',width:75},
		          {field:"PACK_NUM",title:'包号',align:'right',width:50},
		          {field:"S_ID",title:'库房',formatter:eval('window.parent.dz_stocksfmat'),width:120},//editor:{type:'combobox',options:{data:window.parent.dz_stocks,valueField: 'ID',textField:'NAME',colName:'S_ID',dsid:'ds_fx_hand_out2'}}
		          {field:"SHELF_NUM",title:'架号',width:100},
		          {field:"SORT_NUMBER",title:'序号',width:100},
		          {field:"C_ZD_ID",title:'订单号',width:100},
		          {field:"SOURCE_ID",title:'来源订单号',width:100}
		          ]];
	
	//已验区的列表字段
	colns[2]=[[
		          {field:"H_ISBN",title:'书号',width:75},
		          {field:"RUN_ID",title:'该单据的唯一键',hidden:true},
		          {field:"H_NAME",title:'书名',width:200},
		          {field:"H_PRICE",title:'定价',align:'right',width:80},
		          {field:"H_AMOUNT_YF",title:'应发数',align:'right',width:65},
		          {field:"H_AMOUNT",title:'本次应发数',align:'right',width:65},
//		          {field:"USABLED_STOCK",title:'可用库存数',align:'right',width:75},
		          {field:"PACK_NUM",title:'包号',width:50},
		          {field:"S_ID",title:'库房',width:120},
		          {field:"SHELF_NUM",title:'架号',width:100},
		          {field:"SORT_NUMBER",title:'序号',width:100},
		          {field:"C_ZD_ID",title:'订单号',width:100},
		          {field:"SOURCE_ID",title:'来源订单号',width:100}
		          ]];
	
	
	//加载客户信息数据列表
	loadClientDataGrid(divNames[0],dsIds[0],colns[0],width[0],height[0]);
	//初始化待验区，和已验区的数据
	for(var i=1;i<3;i++){
		loadCheckGood(divNames[i],dsIds[i],colns[i],width[i],height[i]);
	}
	//加载下拉框的数据
	loadDzData("sdfy_MXInput");
}


function loadClientDataGrid(divName,dsId,coln,width,height){
	var lastIndex=0;
	$('#'+divName).datagrid({
		iconCls:'icon-save',
		url:_m_url+'framework/loadDataQ?dsId='+dsId,
		width:width, 
		height:height ,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		columns:coln,
		singleSelect:true,
		request:false,
		onClickRow:function(rowIndex,rowData){
			//获取选择中行
			$('#'+divName).datagrid('endEdit', lastIndex);
			$('#'+divName).datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		},
		onClickCell:function(rowIndex, field, value){
			$('#'+divName).datagrid('beginEdit',rowIndex);
			$('#'+divName).attr("colName",field);
			var preIndex=$('#'+divName).attr("currentIndex");
			if(preIndex!=undefined&&preIndex!=null&&preIndex!=null&&rowIndex!=preIndex){
				$('#'+divName).datagrid("endEdit",preIndex);
				$('#'+divName).attr("currentIndex",rowIndex);
				var ed =$('#'+divName).datagrid('getEditor', {index:rowIndex,field:field});
				$('#'+divName).attr("ed",ed);
			}
		},
		onDblClickCell:function(rowIndex, field, value){
			$('#'+divName).datagrid('beginEdit',rowIndex);
			$('#'+divName).attr("colName",field);
			var preIndex=$('#'+divName).attr("currentIndex");
			if(preIndex!=undefined&&preIndex!=null&&preIndex!=null&&rowIndex!=preIndex){
				$('#'+divName).datagrid("endEdit",preIndex);
				$('#'+divName).attr("currentIndex",rowIndex);
				var ed =$('#'+divName).datagrid('getEditor', {index:rowIndex,field:field});
				$('#'+divName).attr("ed",ed);
			}
			
		},onSelect:function(rowIndex,rowData){
			//获取验货区筛选的条件
			var c_id=rowData.C_ID;
			$("#noCheckDataList").attr("cid",c_id);
			configStr="1-C_ID-"+c_id+"-eq|+";
			$.ajax({
				type:'post',
				url:_m_url+'framework/loadDataQ',
				data:{'dsId':dsIds[1],'config':configStr,'page':1,'rows':10000},
				async:false,
				success:function(data){
					$("#noCheckDataList").datagrid('loadData',data);
				}
			});
			$.ajax({
				type:'post',
				url:_m_url+'framework/loadDataQ',
				data:{'dsId':dsIds[2],'config':configStr,'page':1,'rows':10000},
				async:false,
				success:function(data){
					$("#yesCheckDataList").datagrid('loadData',data);
				}
			});
			//验货区加载完数据之后焦点定位在书号上
			$("#H_ISBN").focus();
		}
	});  
	$('#'+divName).datagrid("keyCtr");
}




/********************************
 * 初始化待验区和验货区的datagrid
 ********************************/
function loadCheckGood(divName,dsId,coln,width,height){
	var lastIndex=0;
	$('#'+divName).datagrid({
		iconCls:'icon-save',
		width:width, 
		height:height ,
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		columns:coln,
		singleSelect:true,
		request:false,
		onClickRow:function(rowIndex,rowData){
			//获取选择中行
			$('#'+divName).datagrid('endEdit', lastIndex);
			$('#'+divName).datagrid('beginEdit', rowIndex);
			lastIndex = rowIndex;
		},
		onClickCell:function(rowIndex, field, value){
			$('#'+divName).attr("colName",field);
			var preIndex=$('#'+divName).attr("currentIndex");
			if(preIndex!=undefined&&preIndex!=null&&preIndex!=null){
				$('#'+divName).datagrid("cancelEdit",preIndex);
			}
			$('#'+divName).attr("currentIndex",rowIndex);
			var ed =$('#'+divName).datagrid('getEditor', {index:rowIndex,field:field});
			$('#'+divName).attr("ed",ed);
		},
		onDblClickCell:function(rowIndex, field, value){
			$('#'+divName).attr("colName",field);
			var preIndex=$('#'+divName).attr("currentIndex");
			if(preIndex!=undefined&&preIndex!=null&&preIndex!=null){
				$('#'+divName).datagrid("cancelEdit",preIndex);
			}
			$('#'+divName).attr("currentIndex",rowIndex);
			var ed = $('#'+divName).datagrid('getEditor',{index:rowIndex,field:field});
			$('#'+divName).attr("ed",ed);
		}
	});
	$('#'+divName).datagrid("keyCtr");
	
}


/******************************
 * 起新包
 ******************************/
function addPackNum(){
	$("#PACK_NUM").numberbox('setValue',parseInt($("#PACK_NUM").val())+1);
}


/******************************
 * 取消包号（即重置包号为1）
 ******************************/
function cancelPackNum(){
	
}



/*********************************
 * 客户的搜索
 ******************************/
function cid_query(){
	var cId=$("#C_ID").val();
	configStr="5-C_ID-dz_clients-"+cId+"-inc|+";
	$.ajax({
		type:'post',
		url:_m_url+'framework/loadDataQ',
		data:{'dsId':dsIds[0],'config':configStr,'page':1,'rows':10000},
		async:false,
		success:function(data){
			$("#sdf_DataList").datagrid('loadData',data);
		}
	});
}


/******************************
 *书号的查询查询的是待验区的数据 
 ******************************/
function hisbn_query(){
	 //获取选择的客户的id
	var cidVal=$("#noCheckDataList").attr("cid");
	if(cidVal==undefined||cidVal==null||cidVal==""){
		$.messager.show({title:"操作提示",msg:"你先选择客户!",showType:'slide',timeout: 1800,showSpeed:400});
	}else{
		var hisbnVal=$("#H_ISBN").val();
		configStr="1-C_ID-"+cidVal+"-eq|+1-H_ISBN-"+hisbnVal+"-eq|+";
		$.ajax({
			type:'post',
			url:_m_url+'framework/loadDataQ',
			data:{'dsId':dsIds[1],'config':configStr,'page':1,'rows':10000},
			async:false,
			success:function(data){
				$("#noCheckDataList").datagrid('loadData',data);
			}
		});
		//清空书号
		$("#H_ISBN").val("");
		var length=$("#noCheckDataList").datagrid('getRows').length;
		if(length==0){
			$.messager.show({title:"操作提示",msg:"你输入的书号不存在，请检查是否输入正确!",showType:'slide',timeout: 1800,showSpeed:400});
		}else{
			//查询到数据光标定位在列表第一行数据的本次数列上
			$("#noCheckDataList").datagrid("selectRow",0);
			$("#noCheckDataList").datagrid("beginEdit",0);
			
			$("#noCheckDataList").attr("colName",'H_AMOUNT');
			$("#noCheckDataList").attr("currentIndex",0);
			var ed =$("#noCheckDataList").datagrid('getEditor',{index:0,field:'H_AMOUNT'});
			$("#noCheckDataList").attr("ed",ed);
			
			var input_hmoney=$(ed.target[0]).parent("td").find("input")[0];
			$(input_hmoney).attr("realVal",$(input_hmoney).val());
			$(input_hmoney).focus();
			$(input_hmoney).unbind("keydown");
			$(input_hmoney).bind("keydown",function(event){   
				if(event.keyCode==13){
					//如果书等于应发数，那么直接把该行数据的is_verify更新为1，PACK_NUM更新为当前的包号
					var selectRow=$("#noCheckDataList").datagrid("getSelected");  //获取待验区选中的第一行的数据
					//获取修改后的本次数和原始的本次数的区别
					var realVal=$(input_hmoney).attr("realVal");
					if($(input_hmoney).val()<=parseInt(realVal)){
						var fxHanParam=new Array();
						//获取包号
						var packNo=$($("#bottom").find("#PACK_NUM")[0]).val();
						fxHanParam[0]={"runId":selectRow["RUN_ID"],"packNum":packNo,"cId":cidVal};
						$.ajax({
							url:_m_url+'fho/produceCheckGood',
							type:'post',
							contentType: "application/json;charset=utf-8",
							dataType: "json",
							data:JSON.stringify(fxHanParam),
							success:function(data){
							}
						});
					}else{
						$.messager.alert("验货提示","你输入的本次数不能大于"+realVal,"warning");
						$(input_hmoney).focus();
					}
		 		}
			});
			
		}
	}
}

/******************************
 *书名的查询查询的是待验区的数据 
 ******************************/
function hname_query(){
	 //获取选择的客户的id
	var cidVal=$("#noCheckDataList").attr("cid");
	if(cidVal==undefined||cidVal==null||cidVal==""){
		$.messager.show({title:"操作提示",msg:"你先选择客户!",showType:'slide',timeout: 1800,showSpeed:400});
	}else{
		var hnameVal=$("#H_NAME").val();      //获取输入的书名
		configStr="1-C_ID-"+cidVal+"-eq|+1-H_NAME-"+hnameVal+"-inc|+";
		$.ajax({
			type:'post',
			url:_m_url+'framework/loadDataQ',
			data:{'dsId':dsIds[1],'config':configStr,'page':1,'rows':10000},
			async:false,
			success:function(data){
				$("#noCheckDataList").datagrid('loadData',data);
			}
		});
	}
	
}

/******************************
 *订单号的查询查询的是待验区的数据 
 ******************************/
function orderNo(){
	
}

/******************************
 *库房的查询查询的是待验区的数据 
 ******************************/
function depot_query(){
	
}



/*************************
 * 全部验货
 ***********************/
function allCheckGood(){
	 //获取选择的客户的id
	var cidVal=$("#noCheckDataList").attr("cid");
	
	//获取全部的待验区
	if(cidVal==undefined||cidVal==null||cidVal==""){
		$.messager.show({title:"操作提示",msg:"你先选择客户!",showType:'slide',timeout: 1800,showSpeed:400});
	}else{
		//获取全部验货区的数据
		//获取包号
		var packNo=$($("#bottom").find("#PACK_NUM")[0]).val();   
		var fxHanParam=new Array();
		var allGetRows=$("#noCheckDataList").datagrid("getRows");
		for(var i=0;i<allGetRows.length;i++){
			$("#noCheckDataList").datagrid("endEdit",i);
		}
		for(var i=0;i<allGetRows.length;i++){
			fxHanParam[i]={"runId":allGetRows[i]["RUN_ID"],"packNum":packNo,"cId":cidVal};
		}
		
		$.ajax({
			url:_m_url+'fho/produceCheckGood',
			type:'post',
			contentType: "application/json;charset=utf-8",
			dataType: "json",
			data:JSON.stringify(fxHanParam),
			success:function(data){
			}
		});
	}
	
	
}





